var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), common_Js = require("../../../component/common.js")
import Dialog from '../../../component/vant/dialog/dialog';
import Toast from '../../../component/vant/toast/toast';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    pageSize:10,
    pageIndex: 1,
    recordList:[],
    hasData:false,
    loading: false,
    netWork: false,
    isLoading: true,
    device_id:0,
    state:0,
    record_type:0,
    currentTab:0,
    recordTypes:[0,1,2],
    departType:0,
    showWork:false,
    ssOpenFirework:0,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.type){
      this.data.record_type = parseInt(options.type)
    }
    // this.onHttpInit();
  },
  onChange:function(event) {
    console.log('onChange',event)
    var index = event.detail.index
    var type = this.data.recordTypes[index]
    this.data.record_type = type;
    this.data.pageIndex = 1;
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      recordList:[]
    });
    this.onHttpInit()
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow:function(){
    this.data.pageIndex = 1;
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      hasData:false,
      recordList:[]
    });
    this.onHttpInit()
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/fire/work/my/list', {
      record_type:that.data.record_type,
      pageIndex:that.data.pageIndex,
      pageSize:that.data.pageSize
    }, function (res) {
      console.log("上报记录响应数据", res)
      if (res.data.code == 0) {
        that.data.pageIndex = that.data.pageIndex + 1;

        var isOpenFirework = res.data.data.isOpenFirework;
        if(isOpenFirework != undefined){
          that.setData({
            isOpenFirework: isOpenFirework
          })
        }



        if (res.data.totalcount > 0) {
          var records = res.data.data.records;
          console.log('records',records)
          for (var index in records) {
            that.data.recordList.splice(that.data.recordList.length, 0, records[index]);
          }
          
          that.setData({
            hasData: true,
            recordList: that.data.recordList
          })
        }
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  goCheckRecord(e){
    console.log('goCheckRecord',e);
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '/pages/device/check/index?action=check&id=' + item.id,
    })
  },
  goCheck(e){
    var item = e.currentTarget.dataset.item;
    var submit = e.currentTarget.dataset.submit;
    wx.navigateTo({
      url: '../check/index?id=' + item.id + '&submit=' + submit,
    })
  },  
  goYanShou(e){
    var item = e.currentTarget.dataset.item;
    wx.navigateTo({
      url: '../yanshou/index?id=' + item.id,
    })
  },
  scanCode: function (res) {
    var item = res.currentTarget.dataset.item;
    var usestate = res.currentTarget.dataset.usestate;
    console.log('scanCode res',res)
    var that = this;
    var isLogin = app.getStorageSync("isLogin");
    that.data.from_page = 'scancode'
    if (!isLogin) {
      wx.navigateTo({
        url: '/pages/login/index',
      })
    } else {
      wx.scanCode({
        success: (res) => {
            console.log("扫码结果", res);
            var qrcontent = res.result;
            console.log('扫码结果',qrcontent)
            var json_param = {}
            var linkParam = qrcontent;
            var qr_id = "";
            var qrtype = "";
            
          if(qrcontent.indexOf('https://') >= 0){
            var q = "?" + qrcontent.split("?")[1];
            var json_param = common_Js.getQueryVariable(q);
            var linkParam = "qrtype=range&no=" + json_param.no;
            if(qrcontent.indexOf('/range') >= 0){
              wx.navigateTo({ url: '/pages/device/range/feedRecord/index?id=' + json_param.id })
            } else if(qrcontent.indexOf('/welding') >= 0){
              that.onHttpDeviceDetail(json_param.id,item,usestate);
            }
          }else{
            //{\"qrtype\":\"range\",\"device_id\":" + rangeQrCode.getDeviceId() + ",\"qr_id\":" + rangeQrCode.getId()+"}
            if (qrcontent.indexOf('qrtype') < 0) {//加密过的数据
              qrcontent = CryptoJS.Decrypt(res.result)//解密
              console.log('解密：qrcontent',qrcontent)
              json_param = JSON.parse(qrcontent);
              qrtype = json_param["qrtype"];
              linkParam = "qrtype=" + json_param["qrtype"];
              if(qrcontent.indexOf('range') >= 0){
                linkParam += "&qr_id=" + json_param["qr_id"];
                qr_id = json_param["qr_id"];
              }
            }
            console.log('linkParam',linkParam)
            var is_range_indexof = qrcontent.indexOf('range');
            if (is_range_indexof >= 0) {
              wx.navigateTo({ url: '/pages/device/range/feedRecord/index?qr_id=' + qr_id})
            } else{
              Toast.fail('该二维码不属于该操作');
            }
          }
        },
        fail: (res) => { console.log(res); }
      })
    }
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpDeviceDetail: function (device_id,fireWork,useState) {
    var that = this
    fetchApi.httpPost('/device/welding/detail', {
      device_id: device_id
    }, function (res) {
      console.log("设备详情数据返回", res)
      if (res.data.code == 0) {
          var device = res.data.data.device;
          if(device.isSelf == 0){
            var company_id = app.getStorageSync("shop_id");
            if(device.shopId == company_id){
              // if(device.isSafe == 1){
              //   // wx.navigateTo({ url: '/pages/welding/safeuse/index?id=' + device_id  + '&fId=' + fireWork.id + '&useState=' + useState})
              //   Toast.fail('固定设备，请到固定区域动火操作')
              //   return;
              // }else{
                
              // }
              wx.navigateTo({ url: '/pages/welding/use/index?id=' + device_id  + '&fId=' + fireWork.id + '&useState=' + useState})
            }else{
                Toast.fail('暂无使用权限')
            }
          }else{
            if(device.state == 0){
                wx.navigateTo({ url: '/pages/welding/active/index?id=' + device_id})
            }else{
                var user_id = app.getStorageSync("user_id");
                if(device.leaderWxUserId == user_id){
                  // if(device.isSafe == 1){
                  //   // wx.navigateTo({ url: '/pages/welding/safeuse/index?id=' + device_id + '&fId=' + fireWork.id + '&useState=' + useState})                    
                  //   Toast.fail('固定设备，请到固定区域动火操作')
                  //   return;
                  // }else{
                  // }
                  wx.navigateTo({ url: '/pages/welding/use/index?id=' + device_id + '&fId=' + fireWork.id + '&useState=' + useState})
                }else{
                    Toast.fail('暂无使用权限')
                }
            }
          }
      }else{
          Toast.fail(res.data.msg);
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    })
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.refreshrecordList()
  },
  refreshrecordList:function(){
    this.setData({
      loading: false,
      netWork: false,
      isLoading: true,
      recordList:[]
    })
    this.data.pageIndex = 1
    this.onHttpInit()
  },
  goScanTip(){
    Toast('请安排焊工进行扫码申请焊机');
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.setData({
      isLoading: true
    })
    this.onHttpInit()
  },
  goCheckAnalysis(e){
    var item = e.currentTarget.dataset.item;
    console.log('goCheckAnalysis',e);
    wx.navigateTo({
      url: '../analysis/index?id=' + item.id,
    })
  },
  goCheckMeasures(e){
    var item = e.currentTarget.dataset.item;
    console.log('goCheckMeasures',e);
    if(item.recordType == 0 && (item.fireType == 0 || item.fireType == 1) && item.isHasDeviceReg == 0){
      Toast('焊工申请之后再操作')
      return;
    }else{      
      wx.navigateTo({
        url: '../measure/index?id=' + item.id,
      })
    }
  },
  goRegFork(){
      this.setData({
          showWork: !this.data.showWork,
      })
      // wx.navigateTo({
      //   url: '../addFire/index?type=0',
      // })
  },
  goAddFireFork(e){
      this.goRegFork();
      var type = e.currentTarget.dataset.type;
      if(type == 0){
        wx.navigateTo({
          url: '../addFire/index?type=0',
        })
      } else if(type == 1){
        wx.navigateTo({
          url: '../addSpace/index?type=1',
        })
      } else if(type == 2){
        wx.navigateTo({
          url: '../addHigh/index?type=2',
        })
      }
  },
})